import * as XLSX from 'xlsx'

export const exportExcel = (fileName, Listdata) => {
  //此处tableData.value为表格的数据
  const data = XLSX.utils.json_to_sheet(Listdata)

  //test-data为自定义的sheet表名
  const wb = XLSX.utils.book_new()

  //test.xlsx为自定义的文件名
  XLSX.utils.book_append_sheet(wb, data, fileName)
  XLSX.writeFile(wb, fileName + '.xlsx')
}

export function importExcel(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = (e) => {
      const data = new Uint8Array(e.target.result);
      const workbook = XLSX.read(data, { type: 'array' });
      const worksheet = workbook.Sheets[workbook.SheetNames[0]];
      const result = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
      resolve(result);
    };
    reader.onerror = reject;
    reader.readAsArrayBuffer(file);
  });
}